<?php

namespace app\http\middleware;

use think\facade\Session;
use think\facade\Config;

class Check
{
    private $module;
    private $controller;
    private $action;

    public function handle($request, \Closure $next)
    {
        if ($this->checkWhite()) {//白名单检测
            return $next($request);
        }
        $manager = Session::get('manager');
        if (is_null($manager)){//登录验证
            return redirect('login/login');
        }
        return $next($request);
    }


    private function checkWhite()
    {
        //白名单
        $white = Config::get('white.');
        $this->module = strtolower(request()->module());
        $this->controller = strtolower(request()->controller());
        $this->action = strtolower(request()->action());
        if (empty($white[$this->module]) || empty($white[$this->module][$this->controller])) {
            return false; //不是白名单
        }
        if (in_array($this->action, $white[$this->module][$this->controller])) {
            return true; //白名单
        } else {
            return false; //不是白名单
        }
    }
}
